package yxy.game.pm2.command.hero;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import org.redisson.api.RMap;
import yxy.cherry.battle.module.PropertyData;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.herodata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.cherry.game.base.redis.RedisType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.module.battle.FighterModule;
import yxy.game.pm2.module.battle.PropertyModule;
import yxy.game.pm2.module.hero.HeroHandle;

import java.util.ArrayList;
import java.util.Map;


//武将图鉴获取
@MessageType(proto = Command.HeroHandBook, classtype = HeroMessage.HeroHandBookRequest.class)
public class CMD_HeroHandBook extends Command<HeroMessage.HeroHandBookRequest> {

    @Override
    public void execute(MessageSource messageSource, HeroMessage.HeroHandBookRequest request) {
        try {
            HeroMessage.HeroHandBookResponse.Builder response = HeroMessage.HeroHandBookResponse.newBuilder();
            //处理器
            HeroHandle heroHandle = new HeroHandle();
            //获取缓存
            RMap<Integer, ArrayList<FighterModule>> rMap = RedisManager.getInstance().getConnection(RedisType.REDIS_DATA).client().getMap("INIT:handbook");
            //封装 HeroHandBook

            for (Map.Entry<Integer, ArrayList<FighterModule>> entry : rMap.entrySet()) {

                HeroMessage.HeroHandBook.Builder heroHandBook = HeroMessage.HeroHandBook.newBuilder();

                heroHandBook.setBattleFormation(entry.getKey());//设置势力

                JSONArray arrary = (JSONArray) JSONArray.toJSON(rMap.get(entry.getKey()));
                ArrayList<FighterModule> fighterModules = JSON.parseObject(arrary.toString(), new TypeReference<ArrayList<FighterModule>>() {
                });

                for (FighterModule fighterModule : fighterModules) {
                    //封装 HeroSimpleInfo
                    HeroMessage.HeroSimpleInfo.Builder builder = HeroMessage.HeroSimpleInfo.newBuilder();

                    //武将属性返回
                    PropertyData propertyData = PropertyModule.propertyData(fighterModule.getProperty());
                    builder.setProperty(heroHandle.getHeroProperty(propertyData));

                    //武将资质返回
                    herodata data = DataCenter.getData(fighterModule.getHeroId(), herodata.class);
                    builder.setAptitude(data.getAptitude());

                    //武将基本信息返回
                    HeroMessage.HeroSimple heroSimple = getHeroSimple(fighterModule);
                    builder.setBase(heroSimple);

                    //每个势力多个武将
                    heroHandBook.addHeroSimpleInfos(builder);
                }

                //多个势力返回
                response.addHeroHandBook(heroHandBook);

            }

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void send(MessageSource messageSource, HeroMessage.HeroHandBookResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

    private HeroMessage.HeroSimple getHeroSimple(FighterModule fighterModule) {
        HeroMessage.HeroSimple.Builder builder = HeroMessage.HeroSimple.newBuilder();
        builder.setDataId(fighterModule.getHeroId());
        builder.setLevel(fighterModule.getLevel());
        builder.setStar(fighterModule.getStarlv());
        builder.setBattlepower(fighterModule.getTotalPower());
        return builder.build();
    }
}
